﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using ECCIT;

namespace ECCIT.Data
{
    public abstract class BaseDBHelper
    {
        public abstract string BuildConnectionString(string dataSource, string dbName, string dbUser, string dbPassword);
        public abstract IDbConnection GetDbConnection (string connString);
        public abstract IDbCommand GetDbCommand();
        public abstract IDataAdapter GetDataAdapter(IDbCommand command);
        public abstract IDataParameter GetDataParameter(string parameterName, DataType dataType);
        public abstract IDataParameter GetDataParameter(string parameterName, DataType dataType, int parameterSize);
        public abstract IDataParameter GetDataParameter(string parameterName, DataType dataType, int parameterPrecision, int parameterScale);
        public abstract IDataParameter GetDbParameter(string parameterName);
        public abstract bool TableExists(string tableName);
        public abstract ResultInfo CreateTable(string tableName, List<Column> columns);

        protected int GetDataLengthByDataType(DataType dataType)
        {
            int num1 = -1;
            switch (dataType)
            {
                case DataType.String:
                case DataType.Int:
                case DataType.NText:
                case DataType.Binary:
                case DataType.Decimal:
                case (DataType.NText | DataType.String):
                case (DataType.NText | DataType.Int):
                     num1 = -1;
                     break;
                case DataType.Date:
                    num1 = 10;
                    break;
                case DataType.Time:
                    num1 = 8;
                    break;
                case DataType.DateTime:
                    num1 = 20;
                    break;
                case DataType.YearMonth:
                    num1 = 7;
                    break;
            }
            return num1;
        }

        public IDataParameter GetDataParameter(IField field)
        {
            string parameterName = field.Column.Name;
            DataType type = field.Column.Type;
            int length = field.Column.Length;
            int scale = field.Column.Scale;
            if (length > 0)
            {
                if (scale > 0)
                    return GetDataParameter(parameterName, type, length, scale);
                else
                    return GetDataParameter(parameterName, type, length);
            }
            else
            {
                return GetDataParameter(parameterName, type);
            }
        }

        internal abstract class BaseDBFunction
        {
            public abstract string Length(string columnName);
            public abstract string Substring(string columnName, int startIndex, int? length);
            public abstract string IsNull(string columnName, object replacementValue);
            public abstract string BooleanEquals(string columnName, bool value);
        }
    }
}
